# To change this template, choose Tools | Templates
# and open the template in the editor.

require 'rubygems'
require 'hpricot'
require 'open-uri'
require 'iconv'
require 'forwardable'
require 'util'

class Table
  extend Forwardable
  def_delegators :@data, :each, :[]


  def initialize(options)
    if (options[:url])
      doc = Util.hpricot(options[:url])
    else
      doc = options[:doc]
    end

    lines = doc.search(options[:path])
    @data = [];
    lines.each { |line|  @data.push(parse(line))}
  end

  def column_size
    if @data.empty?
      return -1
    end
    @data[0].size
  end

  def row_size
    @data.size
  end
  
  private
  def parse(line)
    cols = line/"td"
    cols.collect do |col|
      #td.a td.strong or td
      col.at('a').nil? ? (col.at('strong').nil? ? col.inner_text : col.at('strong').inner_text) : col.at('a').inner_text
    end
  end
end
